package com.denlaku.longan.controller.impl;

import com.denlaku.longan.anno.VarBy;
import com.denlaku.longan.controller.ObjectTagRelController;
import com.denlaku.longan.core.Current;
import com.denlaku.longan.http.Resp;
import com.denlaku.longan.http.Return;
import com.denlaku.longan.qo.ObjPermQuery;
import com.denlaku.longan.service.ObjectTagRelService;
import com.denlaku.longan.util.SnowflakeUtils;
import com.denlaku.longan.vo.ObjectTagRel;
import com.denlaku.longan.vo.PageBy;
import com.github.pagehelper.PageHelper;
import org.apache.commons.collections4.ListUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author tianx
 */
@RestController
public class ObjectTagRelControllerImpl implements ObjectTagRelController {
    @Autowired
    private ObjectTagRelService objPermRelService;

    @Override
    public Return<Void> batchAdd(List<ObjectTagRel> objectTagRelList) {
        ListUtils.emptyIfNull(objectTagRelList).forEach(rel -> rel.setId(SnowflakeUtils.nextId()));
        objPermRelService.batchAdd(Current.getTenantId(), objectTagRelList);
        return Resp.success();
    }

    @VarBy
    @Override
    public Return<List<ObjectTagRel>> pageList(ObjPermQuery query, PageBy pageBy) {
        PageHelper.startPage(pageBy.getPage(), pageBy.getSize());
        List<ObjectTagRel> list = objPermRelService.list(query);
        return Resp.success(list);
    }

    @Override
    public Return<Void> delete(ObjPermQuery query) {
        if (query.getId() != null) {
            objPermRelService.delete(query);
        }
        return Resp.success();
    }
}
